package com.mkyong.sys;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="office")
@Cacheable
public class Office {
	private Long id;
	private String name;
	private Office parent;
	private List<User> users = new ArrayList<User>();
	private List<Office> childOffices;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="parent_id")
	public Office getParent() {
		return parent;
	}

	public void setParent(Office parent) {
		this.parent = parent;
	}

	@ManyToMany
	@JoinTable(
			name="user_office"
			,joinColumns={@JoinColumn(name="office_id")}
			,inverseJoinColumns={
				@JoinColumn(name="user_id")
			})
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@OneToMany(mappedBy="parent")
	public List<Office> getChildOffices() {
		return childOffices;
	}

	public void setChildOffices(List<Office> childOffices) {
		this.childOffices = childOffices;
	}

}
